package eddie;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
//import com.thoughtworks.xstream.*;

public class Visual extends javax.swing.JFrame {

    static DefaultListModel gamesListModel = new DefaultListModel();
    static DefaultComboBoxModel genresComboBoxModel = new DefaultComboBoxModel();
    static Visual instance;
    
    public static Visual getInstance() {
        if (instance == null) {
            instance = new Visual();
        }
        return instance;
    }
    
    
    public Visual() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addButton = new javax.swing.JButton();
        listGameScrollPane = new javax.swing.JScrollPane();
        gameList = new javax.swing.JList(gamesListModel);
        removeGameButton = new javax.swing.JButton();
        debugButton = new javax.swing.JButton();
        showGenreLabel = new javax.swing.JLabel();
        genresComboBox = new javax.swing.JComboBox(genresComboBoxModel);
        launchButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Game Organizer");

        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        gameList.setToolTipText("");
        listGameScrollPane.setViewportView(gameList);

        removeGameButton.setText("Remove");
        removeGameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeGameButtonActionPerformed(evt);
            }
        });

        debugButton.setText("Debug");
        debugButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                debugButtonActionPerformed(evt);
            }
        });

        showGenreLabel.setText("Show Only:");

        launchButton.setText("Launch");
        launchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                launchButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listGameScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(showGenreLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(genresComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(addButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(removeGameButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(debugButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(launchButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap(64, Short.MAX_VALUE))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addButton, removeGameButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(listGameScrollPane)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(showGenreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(genresComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(debugButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                        .addComponent(launchButton)
                        .addGap(61, 61, 61)
                        .addComponent(addButton)
                        .addGap(18, 18, 18)
                        .addComponent(removeGameButton)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        new AddGame().setVisible(true);
    }//GEN-LAST:event_addButtonActionPerformed

    private void removeGameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeGameButtonActionPerformed
        try {
            removeGame((Game) gameList.getSelectedValue());
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("No Game to Remove");
        }


    }//GEN-LAST:event_removeGameButtonActionPerformed

    private void launchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_launchButtonActionPerformed
        FileOpener.getInstance().openGame(Main.getGame(gameList.getSelectedIndex()));
        System.out.println("Successful");
    }//GEN-LAST:event_launchButtonActionPerformed

    private void debugButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_debugButtonActionPerformed
        FileOpener.getInstance().openWindowFile();
        //System.out.println(Main.getGame(gameList.getSelectedIndex()).getFileName());
       // String xml = xstream.toXML(games.get(gameList.getSelectedIndex()));
        System.out.println("Successful");
    }//GEN-LAST:event_debugButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton debugButton;
    private static javax.swing.JList gameList;
    private javax.swing.JComboBox genresComboBox;
    private javax.swing.JButton launchButton;
    private javax.swing.JScrollPane listGameScrollPane;
    private javax.swing.JButton removeGameButton;
    private javax.swing.JLabel showGenreLabel;
    // End of variables declaration//GEN-END:variables

    public void addToModel(Game game) {
        gamesListModel.addElement(game);
    }

    public void removeGame(Game game) {
        removeGenre(game);
        Main.removeGame(game);
        gamesListModel.removeElement(game);
    }

    public void addGenre(String genre) {
        if (genresComboBoxModel.getIndexOf(genre) == -1) {
            genresComboBoxModel.addElement(genre);
        }
    }

    public void removeGenre(Game game) {
        boolean alone = true;
        for (int i = 0; i < Main.getGameArraySize(); i++) {
            if (game.getGenre().equals(Main.getGame(i).getGenre()) && Main.indexOfGame(game) != i) {
                alone = false;
            }
        }
        try {
            if (alone) {
                genresComboBoxModel.removeElement(game.getGenre());
            }
        } catch (NullPointerException e) {
        }
    }
}